﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WS1.Filets;
using WS1.Models;
using WS1.Services;

namespace WS1.Controllers
{
    [BasicAuthentication("staff", "BCLyon2024")]

    [RoutePrefix("api/orders")]
    public class OrdersController : ApiController
    {
        // GET: api/Orders
        public AppResult<IEnumerable<Orders>> Get()
        {
            var result = new AppResult<IEnumerable<Orders>>();
            using (var service = new OrdersServices())
            {
                var products = service.GetAll();
                result.Data = products;
            }
            return result;
        }

        // GET: api/Orders/5
        public AppResult<Orders> Get(int id)
        {
            var result = new AppResult<Orders>();

            using (var service = new OrdersServices())
            {
                var product = service.Get(id);
                result.Data = product;
            }
            return result;
        }

        // POST: api/Orders
        public AppResult Post([FromBody] Orders product)
        {
            var result = new AppResult();
            if (ModelState.IsValid)
            {


                try
                {
                    using (var service = new OrdersServices())
                    {
                        service.Add(product);
                    }
                }
                catch (System.Exception ex)
                {
                    result.IsSuccess = false;
                    result.Message = ex.Message;
                }
            }
            else
            {
                result.ModelError(ModelState);
            }
            return result;
        }

        // PUT: api/Orders/id/complete
        [HttpPut]
        [Route("{id}/complete")]
        public AppResult Put(int id, [FromBody] Orders product)
        {
            var result = new AppResult();
            try
            {
                using (var service = new OrdersServices())
                {
                    service.Update(id, product);
                }
            }
            catch (System.Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
            }
            return result;

        }
        // PUT: api/Orders/id/cancel
        [HttpPut]
        [Route("{id}/cancel")]
        public AppResult Put(int id)
        {
            var result = new AppResult();
            try
            {
                using (var service = new OrdersServices())
                {
                    service.Remove(id);
                }
            }
            catch (System.Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
            }
            return result;

        }



        // DELETE: api/Orders/5
        //public void Delete(int id)
        //{
        //}
    }
}
